package com.bfly.mall.product.controller;

import com.bfly.common.ResponseUtil;
import com.bfly.common.page.Pager;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.core.context.PagerThreadLocal;
import com.bfly.core.security.ActionModel;
import com.bfly.core.security.PermissionCheck;
import com.bfly.mall.product.dto.*;
import com.bfly.mall.product.entity.*;
import com.bfly.mall.product.service.IFreightTemplateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 运费模板Controller
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/9/21 19:42
 */
@RestController
@RequestMapping(value = "/manage/freight")
public class FreightTemplateController extends BaseController {

    @Autowired
    private IFreightTemplateService freightTemplateService;

    /**
     * 所有产品名称等信息
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/21 19:43
     */
    @GetMapping(value = "/all")
    @PermissionCheck(required = false)
    public void allFreightTemplateName(HttpServletResponse response) {
        Map<String, Object> param = new HashMap<>();
        param.put("is_deleted", false);
        List<FreightTemplate> list = freightTemplateService.getList(param);
        List<FreightTemplateNameDTO> templateNames = new ArrayList<>();
        FreightTemplateNameDTO nameDTO;
        for (FreightTemplate template : list) {
            nameDTO = new FreightTemplateNameDTO();
            nameDTO.setId(template.getId());
            nameDTO.setName(template.getName());
            templateNames.add(nameDTO);
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(templateNames));
    }

    /**
     * 运费模板列表
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/4/27 14:18
     */
    @GetMapping(value = "/list")
    public void listFreightTemplate(HttpServletRequest request, HttpServletResponse response) {
        PagerThreadLocal.set(request);
        Map<String, Object> param = new HashMap<>();
        param.put("is_deleted", false);
        Pager<FreightTemplate> pager = freightTemplateService.getPage(param);
        Pager<FreightTemplateListDTO> dtoPager = new Pager<>(pager.getPageNo(), pager.getPageSize(), pager.getTotalCount());

        if (pager.getData() != null) {
            List<FreightTemplateListDTO> list = new ArrayList<>();
            List<FreightTemplatePricing> pricingList;
            List<FreightFreeConditions> conditionsList;
            for (FreightTemplate template : pager.getData()) {
                FreightTemplateListDTO templateListDTO = new FreightTemplateListDTO();
                BeanUtils.copyProperties(template, templateListDTO);

                pricingList = template.getPricings();
                List<FreightTemplatePricingListDTO> pricings = new ArrayList<>();
                pricingList.forEach(pricing -> {
                    FreightTemplatePricingListDTO pricingListDTO = new FreightTemplatePricingListDTO();
                    BeanUtils.copyProperties(pricing, pricingListDTO);
                    pricingListDTO.setPricingMethod(template.getPricingMethod());
                    pricings.add(pricingListDTO);
                });
                templateListDTO.setPricings(pricings);

                conditionsList = template.getConditions();
                List<FreightFreeConditionListDTO> conditions = new ArrayList<>();
                conditionsList.forEach(condition -> {
                    FreightFreeConditionListDTO conditionListDTO = new FreightFreeConditionListDTO();
                    BeanUtils.copyProperties(condition, conditionListDTO);
                    conditionListDTO.setPricingMethod(condition.getPricingMethod());
                    conditions.add(conditionListDTO);
                });
                templateListDTO.setConditions(conditions);
                list.add(templateListDTO);
            }
            dtoPager.setData(list);
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(dtoPager));
    }

    /**
     * 新增运费模板
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/4/27 14:23
     */
    @PostMapping(value = "/add")
    @ActionModel(value = "新增运费模板")
    public void addFreightTemplate(@RequestBody @Valid FreightTemplateFormDTO formDTO, BindingResult result, HttpServletResponse response) {
        validData(result);
        freightTemplateService.save(formDTO);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 编辑运费模板
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/4/27 14:23
     */
    @PostMapping(value = "/edit")
    @ActionModel(value = "编辑运费模板")
    public void editFreightTemplate(@RequestBody @Valid FreightTemplateFormDTO formDTO, BindingResult result, HttpServletResponse response) {
        validData(result);
        freightTemplateService.edit(formDTO);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 删除运费模板
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/4/27 14:55
     */
    @PostMapping(value = "/delete")
    @ActionModel(value = "删除运费模板")
    public void delFreightTemplate(HttpServletResponse response, @RequestBody Integer... ids) {
        freightTemplateService.remove(ids);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 运费模板详情
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/4/28 13:39
     */
    @GetMapping(value = "/detail")
    public void detailFreightTemplate(@RequestParam("templateId") int templateId, HttpServletResponse response) {
        FreightTemplate template = freightTemplateService.get(templateId);
        FreightTemplateFormDTO formDTO = new FreightTemplateFormDTO();
        if (template != null && !template.isDeleted()) {
            formDTO.setName(template.getName());
            formDTO.setFreeConditions(template.isFreeConditions());
            formDTO.setFreeShipping(template.isFreeShipping());
            formDTO.setPricingMethod(template.getPricingMethod());
            formDTO.setId(template.getId());

            List<FreightTemplatePricing> pricings = template.getPricings();
            List<FreightTemplatePricingFormDTO> pricingFormDTOS = new ArrayList<>();
            pricings.forEach(pricing -> {
                FreightTemplatePricingFormDTO dto=new FreightTemplatePricingFormDTO();
                BeanUtils.copyProperties(pricing,dto);
                pricingFormDTOS.add(dto);
            });
            formDTO.setPricings(pricingFormDTOS);

            List<FreightFreeConditions> conditions =template.getConditions();
            List<FreightFreeConditionsFormDTO>  conditionsFormDTOS = new ArrayList<>();
            conditions.forEach(condition -> {
                FreightFreeConditionsFormDTO dto=new FreightFreeConditionsFormDTO();
                BeanUtils.copyProperties(condition,dto);
                conditionsFormDTOS.add(dto);
            });
            formDTO.setConditions(conditionsFormDTOS);
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(formDTO));
    }
}